﻿using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository.Base;
using System.Repository.DataBase.Models;
using Microsoft.EntityFrameworkCore;

namespace System.Repository
{
    /// <summary>
    /// 数据库帮助类
    /// </summary>
    public class DbHelper
    {
        private static readonly string Connstring = ConfigurationManager.ConnectionStrings["DbBackup"].ConnectionString;
        public static int ExecuteSqlCommand(string cmdText)
        {
            using (DbConnection conn = new SqlConnection(Connstring))
            {
                DbCommand cmd = new SqlCommand();
                PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, null);
                return cmd.ExecuteNonQuery();
            }
        }


        private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (isOpenTrans != null)
                cmd.Transaction = isOpenTrans;
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                cmd.Parameters.AddRange(cmdParms);
            }
        }
    }
    /// <summary>
    ///数据库备份 接口 仓库实现 
    /// </summary>	
    public class SysDbBackupRepository : RepositoryDomain<SysDbBackupEntity, Sys_DbBackup>, ISysDbBackupRepository
    {
        public SysDbBackupRepository(IDbContextConfig config) : base(config)
        {
        }
        public override bool Insert(SysDbBackupEntity dbBackupEntity)
        {
            DbHelper.ExecuteSqlCommand(string.Format("backup database {0} to disk ='{1}'", dbBackupEntity.DbName, dbBackupEntity.FilePath));

            //TODO 待处理
            //  dbBackupEntity.FileSize = FileHelper.ToFileSize(FileHelper.GetFileSize(dbBackupEntity.FilePath));

            dbBackupEntity.FilePath = "/Resource/DbBackup/" + dbBackupEntity.FileName;
            Sys_DbBackup model = AutoCopy(null, dbBackupEntity);
            Dbcontext.Entry(model).State = EntityState.Added;
            return Dbcontext.SaveChanges() > 0;

        }

        public override bool Update(SysDbBackupEntity entity)
        {
            Sys_DbBackup model = GetModel(entity.Id);
            if (model == null)
                return false;
            Dbcontext.Set<Sys_DbBackup>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, entity);
            return Dbcontext.SaveChanges() > 0;
        }

        public override bool Delete(string key)
        {
            var entitys = Dbcontext.Set<Sys_DbBackup>().Where((t => t.Id == (string)key)).FirstOrDefault();
            if (entitys != null)
            {
                // TODO 待处理
                //if (FileHelper.IsExistFileWeb(entitys.FilePath))
                //{
                //    FileHelper.DeleteFile(entitys.FilePath);
                //}
                Dbcontext.Entry(entitys).State = EntityState.Deleted;
                return Dbcontext.SaveChanges() > 0;
            }
            else
            {
                return true;
            }
            //return Delete(t => t.Id == (string)key) > 0;
        }


       
    }

}
